package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.service.IUserService;
import com.hmdp.utils.RedisConstants;
import com.hmdp.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    @Autowired
    private FollowMapper followMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private IUserService userService;

    /**
     * 关注用户
     * @param id
     * @param isFollow
     * @return
     */
    @Override
    public Result FocusOnUser(Long id, Boolean isFollow) {
        //1.获取当前用户id
        Long userId = UserHolder.getUser().getId();
        String key = RedisConstants.FOLLOW_KEY + userId;
        //2.判断当前业务是关注用户还是取关用户
        if (isFollow){
            //3.1关注用户逻辑
            Follow follow = new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(id);
            follow.setCreateTime(LocalDateTime.now());
            boolean isSuccess =  followMapper.focusUser(follow);
            if (isSuccess){
                //添加成功，那么向redis里面也添加一份数据
                stringRedisTemplate.opsForSet().add(key,id.toString());
            }
        }else {
            //3.2取关用户
            Follow follow = new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(id);
            follow.setCreateTime(LocalDateTime.now());
            boolean isSuccess = followMapper.unfocusUser(follow);
            if (isSuccess){
                //如果删除成功，那么也将redis里面的数据也一同删除
                stringRedisTemplate.opsForSet().remove(key,id.toString());
            }
        }
        return Result.ok();
    }

    /**
     * 查看是否关注用户
     * @param followUserId
     * @return
     */
    @Override
    public Result isFocusONUser(Long followUserId) {
        //1.获取当前用户id
        Long userId = UserHolder.getUser().getId();
        Follow follow = new Follow();
        follow.setFollowUserId(followUserId);
        follow.setUserId(userId);
        //2.判断是否关注用户
        Integer count = followMapper.isFocusONUser(follow);
        return Result.ok(count > 0);
    }

    /**
     * 查看共同好友
     * @param followUserId
     * @return
     */
    @Override
    public Result commonFollow(Long followUserId) {
        //1.获取当前用户id
        Long userId = UserHolder.getUser().getId();
        //2.获取两人的redis的key值
        String userKey = RedisConstants.FOLLOW_KEY + userId;
        String followKey = RedisConstants.FOLLOW_KEY + followUserId;
        //3.查看当前两人的共同关注
        Set<String> commonFollow = stringRedisTemplate.opsForSet().intersect(userKey, followKey);
        //4.将set转化成Long
        if (commonFollow == null || commonFollow.isEmpty()){
            //如果没有共同关注，那么就返回空list
            return Result.ok(Collectors.toList());
        }
        //5.解析集合
        List<Long> ids = commonFollow.stream().map(Long::valueOf).collect(Collectors.toList());
        //6.查询用户
        List<UserDTO> userDTOS = userService.listByIds(ids)
                .stream()
                .map(user ->
                        BeanUtil.copyProperties(user, UserDTO.class)).collect(Collectors.toList());
        return Result.ok(userDTOS);
    }


}
